using System.Collections.Generic;
using System.Linq;
using WatiN.Core;

namespace YatFram.Browser.Watin
{
    public class WatinCheckBox : IMultipleOptions
    {
        private readonly IEnumerable<CheckBox> checkBoxes;

        public WatinCheckBox(IEnumerable<CheckBox> checkBoxes)
        {
            this.checkBoxes = checkBoxes;
        }

        public IEnumerable<string> CheckedValues
        {
            get
            {
                var checkedOptions = checkBoxes.Where(option => option.Checked);
                return checkedOptions.Count() == 0 ? new string[]{} :  checkedOptions.Select(option => option.GetAttributeValue("value"));
            }
        }

        public void Check(string value)
        {
            checkBoxes.Single(option => option.GetAttributeValue("value") == value).Checked = true;
        }

        public void Uncheck(string value)
        {
            checkBoxes.Single(option => option.GetAttributeValue("value") == value).Checked = false;
        }
    }
}